<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="供应商名称" prop="orgName">
            <el-input v-model="dataForm.orgName" placeholder="供应商名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类型" prop="orgCategory">
            <!--<el-input v-model="dataForm.orgCategoryName" placeholder="类型"></el-input>-->
            <dictionary-select dictType="org_category" :dictionaryCode.sync="dataForm.orgCategory" :dictionaryName.sync="dataForm.orgCategoryName"></dictionary-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="税号" prop="taxNo">
            <el-input v-model="dataForm.taxNo" placeholder="税号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开户行" prop="hplBank">
            <el-input v-model="dataForm.hplBank" placeholder="开户行"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="银行账户" prop="bankNo">
            <el-input v-model="dataForm.bankNo" placeholder="银行账户"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系人" prop="contactPerson">
            <el-input v-model="dataForm.contactPerson" placeholder="联系人"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话" prop="contactTel">
            <el-input v-model="dataForm.contactTel" placeholder="联系电话"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="地址" prop="areaCodeList" >
            <el-row>
              <el-col :span="12">
                <area-select :areaCodeList.sync="dataForm.areaCodeList" :areaListName.sync="areaListName" ref="areaList"></area-select>
              </el-col>
              <el-col :span="12">
                <el-input v-model="dataForm.hplAddr" placeholder="详细地址"></el-input>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
      </el-row>
    <el-form-item label="备注信息" prop="remarks">
      <el-input type="textarea" v-model="dataForm.remarks" placeholder="备注信息"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isMobile, isPhone } from '@/utils/validate'
  import areaSelect from '@/components/common/area-select'
  import { getCaption } from '@/utils/index'
  import DictionarySelect from '@/components/common/dictionary-select'
  const defaultForm = {
    orgCode: 0,
    orgName: '',
    orgCategory: '',
    taxNo: '',
    hplBank: '',
    bankNo: '',
    contactPerson: '',
    contactTel: '',
    areaCodeList: [],
    hplAddr: '',
    status: '',
    createBy: '',
    createDate: '',
    updateBy: '',
    updateDate: '',
    remarks: ''
  }

  var validateMobile = (rule, value, callback) => {
    if (isMobile(value) || isPhone(value)) {
      callback()
    } else {
      callback(new Error('手机号格式错误'))
    }
  }
  var validateCard = (rule, value, callback) => {
    if (value % 1 === 0) { // 检测是否是整数
      callback()
    } else {
      callback(new Error('银行卡格式错误'))
    }
  }
  var validateArea = (rule, value, callback) => {
    if (value) {
      if (value.length !== 3) {
        callback(new Error('详细地址不能为空'))
      } else {
        callback()
      }
    } else {
      callback()
    }
  }
  export default {
    data () {
      return {
        visible: false,
        dataForm: JSON.parse(JSON.stringify(defaultForm)),
        areaListName: '',
        dataRule: {
          orgName: [
            { required: true, message: '供应商名称不能为空', trigger: 'blur' }
          ],
          orgCategory: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          contactTel: [
            { validator: validateMobile, trigger: 'blur' }
          ],
          bankNo: [
            { validator: validateCard, trigger: 'blur' }
          ],
          areaCodeList: [
            { validator: validateArea, message: '请选择省市区地址', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      DictionarySelect,
      areaSelect
    },
    methods: {
      init (id) {
        this.dataForm.orgCode = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orgCode) {
            this.$http({ // `/basedata/supplierinfo/info/${this.dataForm.splCode}`
              url: this.$http.adornUrl(`${this.$API.BASE_INFO.SUPPLIER_INFO.INFO}/${this.dataForm.orgCode}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data) {
                this.dataForm = data
                this.$refs.areaList.areaDetail(data.areaCodeList)
                if (this.dataForm.hplAddr) {
                  this.dataForm.hplAddr = (getCaption(this.dataForm.hplAddr, ':'))
                }
              }
            })
          } else {
            this.dataForm = JSON.parse(JSON.stringify(defaultForm))
            this.$refs.areaList.init()
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        console.log(this.dataForm.orgCode)
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({// `/basedata/supplierinfo/${!this.dataForm.splCode ? 'save' : 'update'}`
              url: this.$http.adornUrl(`${!this.dataForm.orgCode ? this.$API.BASE_INFO.SUPPLIER_INFO.SAVE : this.$API.BASE_INFO.SUPPLIER_INFO.UPDATE}`),
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            }).then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
